package com.zhia.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhia.demo.dao.UserLikeDao;
import com.zhia.demo.entity.UserLikeEntity;
import com.zhia.demo.service.RecoverService;
import com.zhia.demo.service.ReviewService;
import com.zhia.demo.service.UserLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author DONG
 * @create 2023/11/5 13:11
 */
@Service
public class UserLikeServiceImpl extends ServiceImpl<UserLikeDao, UserLikeEntity> implements UserLikeService {
    @Autowired
    RecoverService recoverService;

    @Autowired
    ReviewService reviewService;

    @Override
    public boolean getUserIsLikeReview(Integer userId, String reviewId) {
        int count = this.count(new QueryWrapper<UserLikeEntity>().eq("user_id", userId).eq("review_id", reviewId));
        return count > 0;
    }

    @Override
    public void addLike(String reviewId, Integer userId) {
        if (reviewId.startsWith("p")) {
            reviewService.likeAdd(Integer.parseInt(reviewId.substring(1)));
        } else {
            recoverService.likeAdd(Integer.parseInt(reviewId.substring(1)));
        }
        UserLikeEntity userLikeEntity = new UserLikeEntity();
        userLikeEntity.setUserId(userId);
        userLikeEntity.setReviewId(reviewId);
        this.save(userLikeEntity);
    }

    @Override
    public void cancelLike(String reviewId, Integer userId) {
        if (reviewId.startsWith("p")) {
            reviewService.likeDec(Integer.parseInt(reviewId.substring(1)));
        } else {
            recoverService.likeDec(Integer.parseInt(reviewId.substring(1)));
        }

        this.remove(new QueryWrapper<UserLikeEntity>().eq("review_id", reviewId).eq("user_id", userId));
    }
}
